﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;

namespace BeiXun.Shared.UserControls
{
    public partial class TopNavBar : UserControl
    {
        private const string ITEM_HTML = "<li><a class='{0}' _nvs='{3}' href='{2}' target='{5}' id='topnav_{4}'><span>{1}</span></a></li>";
        private const string CONTAINER_HTML = "<ul class='navigation'>{0}</ul>";
        private const string SELECTED_CSS_CLASS = "current";
        private const string NORMAL_CSS_CLASS = "";

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void Render(HtmlTextWriter writer)
        {
            SiteMapNode root = this.topSitemap.Provider.RootNode;
            StringBuilder sb = new StringBuilder();

            foreach (SiteMapNode node in root.ChildNodes)
            {
                if (node.Description == "100000" || node.Description == "200000")//默认加上首页，修改密码放在系统设置里面，所以默认显示
                {
                    bool bCurrentNode = (node == this.topSitemap.Provider.CurrentNode);
                    string url = node.Url.TrimEnd('#', '?');
                    string target = node["target"];

                    if (target != null) target = target.Trim();
                    if (String.IsNullOrEmpty(target)) target = "_self";

                    sb.AppendFormat(ITEM_HTML + System.Environment.NewLine,
                        bCurrentNode ? SELECTED_CSS_CLASS : NORMAL_CSS_CLASS,
                        Server.HtmlEncode(node.Title),
                        url,
                        SELECTED_CSS_CLASS,
                        node.Url.GetHashCode(),
                        target);
                }
                else//判读用户是否有父菜单权限（当用户具有父菜单的子菜单权限时就显示父菜单）
                {
                    if (Session["UserPurviews"] != null)
                    {
                        string strPur = Session["UserPurviews"].ToString();

                        //系统管理
                        if (node.Description == "200000")
                        {
                            if (strPur.Contains("201000") || strPur.Contains("202000") || strPur.Contains("201010") || strPur.Contains("201020")
                                || strPur.Contains("202010")
                                || strPur.Contains("203000") || strPur.Contains("203010"))
                            {
                                bool bCurrentNode = (node == this.topSitemap.Provider.CurrentNode);
                                string url = node.Url.TrimEnd('#', '?');
                                string target = node["target"];

                                if (target != null) target = target.Trim();
                                if (String.IsNullOrEmpty(target)) target = "_self";

                                sb.AppendFormat(ITEM_HTML + System.Environment.NewLine,
                                    bCurrentNode ? SELECTED_CSS_CLASS : NORMAL_CSS_CLASS,
                                    Server.HtmlEncode(node.Title),
                                    url,
                                    SELECTED_CSS_CLASS,
                                    node.Url.GetHashCode(),
                                    target);
                            }
                        }
                        // 个人绩效管理
                        if (node.Description == "300000")
                        {
                            if (strPur.Contains("301000") || strPur.Contains("301010") || strPur.Contains("301020")
                                || strPur.Contains("301030") || strPur.Contains("302000") || strPur.Contains("303000"))
                            {
                                bool bCurrentNode = (node == this.topSitemap.Provider.CurrentNode);
                                string url = node.Url.TrimEnd('#', '?');
                                string target = node["target"];

                                if (target != null) target = target.Trim();
                                if (String.IsNullOrEmpty(target)) target = "_self";

                                sb.AppendFormat(ITEM_HTML + System.Environment.NewLine,
                                    bCurrentNode ? SELECTED_CSS_CLASS : NORMAL_CSS_CLASS,
                                    Server.HtmlEncode(node.Title),
                                    url,
                                    SELECTED_CSS_CLASS,
                                    node.Url.GetHashCode(),
                                    target);
                            }
                        }

                        // 流程管理
                        if (node.Description == "400000")
                        {
                            if (strPur.Contains("401000") || strPur.Contains("401010"))
                            {
                                bool bCurrentNode = (node == this.topSitemap.Provider.CurrentNode);
                                string url = node.Url.TrimEnd('#', '?');
                                string target = node["target"];

                                if (target != null) target = target.Trim();
                                if (String.IsNullOrEmpty(target)) target = "_self";

                                sb.AppendFormat(ITEM_HTML + System.Environment.NewLine,
                                    bCurrentNode ? SELECTED_CSS_CLASS : NORMAL_CSS_CLASS,
                                    Server.HtmlEncode(node.Title),
                                    url,
                                    SELECTED_CSS_CLASS,
                                    node.Url.GetHashCode(),
                                    target);
                            }
                        }
                    }
                }
            }

            writer.Write(String.Format(CONTAINER_HTML, sb.ToString()));
        }
    }
}